﻿using System;
using System.Linq;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class removePlayerFromTeam : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        public string IPadId { get; set; }
        public int TeamID { get; set; }
        public removePlayerFromTeam(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            try 
            { 
                Player player = (from x in dbContext.Players where x.IPadID == IPadId select x).FirstOrDefault();
                PlayersTeam pt = (from x in dbContext.PlayersTeams where x.PlayerID == player.PlayerID && x.TeamID == TeamID select x).FirstOrDefault();
                dbContext.PlayersTeams.DeleteObject(pt);
                dbContext.SaveChanges();
                return "OK";
            }
            catch (Exception)
            {
                return "DONT EXISTS";
            }
        }

        public override void InitializeProperties()
        {
           
            try
            {
                IPadId = Parameters["ipadid"];
                TeamID = Convert.ToInt32(Parameters["teamid"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}